package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;

import com.cmas.platform.common.annotation.OperatorDataScope;
import com.cmas.platform.domain.ApproveRecord;
import com.cmas.platform.domain.UserRecord;
import com.cmas.platform.vo.RegionVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.Region;
import com.cmas.platform.service.RegionService;

import javax.validation.constraints.NotBlank;


/**
 * 地区Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-14
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/region")
public class RegionController extends BaseController {

    @Autowired
    private RegionService regionService;

    /**
     * 查询地区列表
     */

    @PreAuthorize("@ss.hasPermi('platform:region:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(Region region) {
        startPage();
        List<Region> list = regionService.selectRegionList(region);
        return R.data(getDataTable(list));
    }

    /**
     * 获取地区详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:region:query')")
    @GetMapping(value = "/{id}")
    public R<Region> detail(@PathVariable("id") String id) {
        return R.data(regionService.getById(id));
    }


    /**
     * 获取地区详细信息(包含版块、地图)
     */
    @PreAuthorize("@ss.hasPermi('platform:region:query')")
    @GetMapping(value = "/regionDetail/{id}")
    public R<RegionVo> regionDetail(@PathVariable("id") String id) {
        return R.data(regionService.getRegionDetail(id));
    }


    /**
     * 审核地区
     */
    @PreAuthorize("@ss.hasPermi('platform:region:approve')")
    @Log(title = "审核地区", businessType = BusinessType.INSERT)
    @PostMapping("/approve")
    public R approve(@RequestBody @Validated ApproveRecord approveRecord) {
        regionService.approve(approveRecord);
        return R.success("审核成功");
    }

    /**
     * 新增地区
     */
    @PreAuthorize("@ss.hasPermi('platform:region:add')")
    @Log(title = "地区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Region region) {
        if (regionService.addRegion(region)) {
            return R.success("新增成功");
        }
        return R.fail("请先添加固定版块");
    }

    /**
     * 修改地区表
     */
    @PreAuthorize("@ss.hasPermi('platform:region:edit')")
    @Log(title = "地区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Region region) {
        return R.data(regionService.updateRegion(region));
    }

    /**
     * 删除地区
     */
    @PreAuthorize("@ss.hasPermi('platform:region:remove')")
    @Log(title = "地区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(regionService.deleteRegionByIds(ids));
    }


    /**
     * 校验版块编码的唯一性
     */
    @PreAuthorize("@ss.hasPermi('platform:region:query')")
    @Log(title = "地区管理", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/checkCodeUnique/{code}/{id}", "/checkCodeUnique/{code}"})
    public R checkCodeUnique(@NotBlank(message = "地区编码不允许为空") @PathVariable("code") String code, @PathVariable(value = "id", required = false) String id) {
        Region region = new Region();
        region.setCode(code);
        region.setId(id);
        return R.data(regionService.checkCodeUnique(region));
    }


    /**
     * 导出地区列表
     */
    @PreAuthorize("@ss.hasPermi('platform:region:export')")
    @Log(title = "地区管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Region region) {
        List<Region> list = regionService.list();
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        return util.exportExcel(list, "地区表数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }


    // ***用户系统位置戳****

    /**
     *  选择地区
     */
    @Log(title = "选择地区", businessType = BusinessType.INSERT)
    @PostMapping("/chooseRegion")
    public R chooseRegion(String regionId) {
        if (regionService.chooseRegion(regionId)==0) {
            return R.data(false);
        }
        return R.data(true);
    }

    /**
     * 获取地区详细信息(包含版块、地图)
     */
    @GetMapping(value = "/getRegionByUser")
    public R<RegionVo> getRegionByUser() {
        return R.data(regionService.getRegionByUser());
    }
}
